//
//  DataFrameWeb.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/26.
//

#import "DataFrameWeb.h"
#import <StoreKit/StoreKit.h>
#import "Auth.h"
#import "AppDelegate.h"
#import "LocationUpdata.h"
#import "Setting.h"
#import "Login.h"
@interface DataFrameWeb ()<DataFrameDelegate>
@property(nonatomic, strong)DataFrameWebTool *tool;
@end
@implementation DataFrameWeb


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    [self navUI];
    if ([self.name isEqualToString:@""]) {
        self.navTitle.text = @"Champoin Peso";
    }else{
        self.navTitle.text = self.name;
    }
    
    [self.back_btn addTarget:self action:@selector(backTo:) forControlEvents:UIControlEventTouchUpInside];
    
    if(!self.tool){
        self.tool = [DataFrameWebTool new];
        self.tool.delegate = self;
    }
    [self.tool WebConfigMake:self.view path:self.frameTo];
    // Do any additional setup after loading the view.
}
-(void)back{
    
}
- (void)backTo:(UIButton *)sender {
    //退出
    if(self.tool.webTool.canGoBack){
        [self.tool.webTool goBack];
    } else {
        NSMutableArray *arrs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        if(arrs.count > 2){
            if([arrs[arrs.count - 2] isKindOfClass:[Auth class]]){
                [arrs removeObjectAtIndex:arrs.count - 2];
            }
        }
        self.navigationController.viewControllers = arrs;
        [self.navigationController popViewControllerAnimated:NO];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    };
}


#pragma mark -delegate-
- (void)begana:(NSString *)msg {
    
    if([msg containsString:@"http"]){
        DataFrameWeb *web = [DataFrameWeb new];
        web.frameTo = msg;
        [self.navigationController pushViewController:web animated:NO];
    }else{
        [self.navigationController popToRootViewControllerAnimated:NO];
        if ([msg containsString:@"jxe://ilgb.bbh.bjwv/wasouta"]) {
            Setting *vc = [Setting new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([msg containsString:@"jxe://ilgb.bbh.bjwv/wasoutb"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:0];
        }else if ([msg containsString:@"jxe://ilgb.bbh.bjwv/wasoutc"]) {
            Login *login = [Login new];
            [self presentViewController:login animated:YES completion:nil];
        }else if ([msg containsString:@"jxe://ilgb.bbh.bjwv/wasoutd?wasoutf=1"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:1];
            NSArray *item = [msg componentsSeparatedByString:@"="];
            NSString *tab = item[1];
            NSDictionary *userInfo = @{@"tab": tab};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoOrder" object:self userInfo:userInfo];
        }else if ([msg containsString:@"jxe://ilgb.bbh.bjwv/wasoute?wasoutg=2"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:0];
            NSArray *item = [msg componentsSeparatedByString:@"="];
            NSString *pid = item[1];
            NSDictionary *userInfo = @{@"pid": pid};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoDetail" object:self userInfo:userInfo];
        }
    }
}

- (void)itb {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)clothesd {

    [self.navigationController popToRootViewControllerAnimated:NO];
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    TabBarViewController* vc =  delegate.root;
    [vc selectIndex:0];
}

- (void)fore {

    [self.navigationController popToRootViewControllerAnimated:NO];
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    TabBarViewController* vc =  delegate.root;
    [vc selectIndex:2];
}

- (void)anf {

    [self.navigationController popToRootViewControllerAnimated:NO];
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    TabBarViewController* vc =  delegate.root;
    [vc selectIndex:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoLogin" object:self];

}

- (void)trickg:(NSString *)tel {
    
    NSString *telStr = @"";
    telStr = [telStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    telStr = [telStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"%^{}\"[]|\\<> "].invertedSet];
    NSMutableString * url= [[NSMutableString alloc] initWithFormat:@"tel://%@",telStr];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
}

- (void)accidentallyh {
    [SKStoreReviewController requestReview];
}

- (void)andi {
    
    [self startMark:@"10"];
}

- (void)songsFactj {
    [[LocationUpdata sharedInstance] startMarkTag];
}

- (void)whok {
//    [self startMark:@"8"];
    [self startMarkFor8];
}

- (void)changeTitleName:(NSString *)name {
    if ([CPS(name) isEqualToString:@""]) {
        self.navTitle.text = @"Champoin Peso";
    }else{
        self.navTitle.text = name;
    }
}
@end
